package com.guigu.guliorder.component;

import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static com.guigu.gulicommon.constant.MQConstant.*;

/**
 * @author 陈俊杰
 * @version 1.0
 * @since 2024/9/30 22:56
 */
@Log4j2
@Component
public class RabbitMqUtils {

    /**
     * 创建订单死信队列
     */
    @Bean
    public Queue orderDeadQueue() {
        Map<String, Object> arg = new HashMap<>();
        arg.put("x-dead-letter-exchange", ORDER_EVENT_EXCHANGE);
        arg.put("x-dead-letter-routing-key", ORDER_RELEASE_ROUTING_KEY);
        arg.put("x-message-ttl", 60000);

        return new Queue(ORDER_DELAY_QUEUE, true, false, false, arg);
    }

    /**
     * 解锁订单队列
     */
    @Bean
    public Queue orderReleaseQueue() {

        return new Queue(ORDER_RELEASE_QUEUE, true, false, false);
    }

    /**
     * 订单事件交换机
     */
    @Bean
    public Exchange orderEventExchange() {

        return new TopicExchange(ORDER_EVENT_EXCHANGE, true, false);
    }

    /**
     * 订单创建队列
     *
     * @return 绑定关系
     */
    @Bean
    public Binding orderCreatBinding() {
        return new Binding(ORDER_DELAY_QUEUE,
                Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE,
                ORDER_CREATE_ROUTING_KEY, null);
    }

    /**
     * 订单解锁队列
     *
     * @return 绑定关系
     */
    @Bean
    public Binding orderReleaseBinding() {
        return new Binding(ORDER_RELEASE_QUEUE,
                Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE,
                ORDER_RELEASE_ROUTING_KEY, null);
    }


}


